package com.ocom.wxapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.exposeapi.LargeScreenRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.DateUtils;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.utils.StringUtils;
import com.ocom.wxapi.client.MjRecodeClientService;
import com.ocom.wxapi.service.AccessRecordPageService;
import com.ocom.wxapi.vo.EntryExitRecordsRequest;
import com.ocom.wxapi.vo.RecentlyAcTimeVo;
import com.ocom.wxapi.vo.accessrecordpage.OnShowVo;
import com.ocom.wxapi.vo.accessrecordpage.QueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@Slf4j
@RequestMapping("/AccessRecordPage")
@RestController
public class AccessRecordController {

    @Autowired
    private AccessRecordPageService accessRecordPageService;

    @Autowired
    private MjRecodeClientService mjRecodeClientService;
    //门禁记录（AccessRecordPage）
    //查询（AccessRecord/queryVo）
    @PostMapping(value = "/query")
    public Result query(@Validated QueryVo queryVo){
        if (StringUtils.isNull(queryVo.getComId())){
            log.error("单位编号为空");
            return ResultUtil.error(ResultEnum.Err_203,"单位编号为空");
        }
//        if (StringUtils.isNull(queryVo.getPId())){
//            log.error("人员编号为空");
//            return ResultUtil.error(ResultEnum.Err_203,"人员编号为空");
//        }
//        System.out.println(queryVo);
        return accessRecordPageService.query(queryVo);
    }

    /**
     * 显示页面（AccessRecordPage/onShow）
     * */
    @PostMapping(value = "/onShow")
    public Result onShow(@Validated OnShowVo onShowVo){
        if (StringUtils.isNull(onShowVo.getComId())){
            log.error("单位编号为空");
            return ResultUtil.error(ResultEnum.Err_203,"单位编号为空");
        }
        if (StringUtils.isNull(onShowVo.getPId())){
            log.error("人员编号为空");
            return ResultUtil.error(ResultEnum.Err_203,"人员编号为空");
        }
        return accessRecordPageService.onShow(onShowVo);
    }



    @PostMapping(value = "/recentlyAcTime")
    public Result recentlyAcTime(@Validated RecentlyAcTimeVo recentlyAcTime){
        if (StringUtils.isNull(recentlyAcTime.getComId())){
            log.error("单位编号为空");
            return ResultUtil.error(ResultEnum.Err_203,"单位编号为空");
        }
        LargeScreenRequest largeScreenRequest=new LargeScreenRequest();
        largeScreenRequest.setCompanyId(recentlyAcTime.getComId().toString());
        JSONObject param =new JSONObject();
        param.put("days",7);
        param.put("day", DateUtils.getDate());
        param.put("deptId", recentlyAcTime.getDeptId());
        param.put("areaNo", recentlyAcTime.getAreaNo());
        param.put("type", 1);
        param.put("pName", recentlyAcTime.getPName());
        largeScreenRequest.setParam(param);
        return mjRecodeClientService.recentlyAcTime(largeScreenRequest);
    }


    @PostMapping("/entryExitRecords")
    public Result entryExitRecords(@Valid EntryExitRecordsRequest request){
        LargeScreenRequest largeScreenRequest=new LargeScreenRequest();
        largeScreenRequest.setCompanyId(request.getComId().toString());
        largeScreenRequest.getParam().put("areaId",request.getAreaId());
        largeScreenRequest.getParam().put("deptId",request.getDeptId());
        largeScreenRequest.getParam().put("pName",request.getPName());
        return mjRecodeClientService.entryExitRecords(largeScreenRequest);
    }

}
